﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using JohnnySins.DAL;
using JohnnySins.DAL.Repositories;
using JohnnySins.Entities;
using JohnnySins.Code;

namespace JohnnySins
{
    public partial class MemberPhotos : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        public void LoadData()
        {
            using (var repo = RepositoryFactory.ResolveRepository<PhotoSetRepository>())
            {
                List<PhotoSet> ps = repo.FindActiveMember().ToList();
                if (ps.Count == 0)
                    return;

                List<PhotoSetPhoto> posters = new List<PhotoSetPhoto>();
                foreach (PhotoSet item in ps)
                {
                    if (item.Photos.Count() == 0)
                        continue;

                    PhotoSetPhoto poster = item.Photos.FirstOrDefault(p => p.IsMemberPoster);
                    if (poster == null)
                        poster = item.Photos.ElementAt(0);
                    poster.PhotoSetTitle = item.PhotoSetTitle;
                    posters.Add(poster);
                }

                rptPhotos.DataSource = posters;
                rptPhotos.DataBind();
            }
        }
        
        protected void rptPhotos_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                PhotoSetPhoto rowView = e.Item.DataItem as PhotoSetPhoto;

                HtmlImage img = e.Item.FindControl("imgPhoto") as HtmlImage;
                img.Src = "~/Files/PhotoSets/" + rowView.PhotoSetId.ToString() + "/" + rowView.PhysFileName;

                HyperLink hl = e.Item.FindControl("hlPhotoset1") as HyperLink;
                hl.NavigateUrl = "~/Member/MyPhotosets.aspx?psid=" + rowView.PhotoSetId.ToString();
                hl = e.Item.FindControl("hlPhotoset2") as HyperLink;
                hl.NavigateUrl = "~/Member/MyPhotosets.aspx?psid=" + rowView.PhotoSetId.ToString();
            }
        }

        
      
    }
}